package com.loansystem.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailService {

    @Autowired
    private  JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private  String from;
    private  String content = "您的验证码为---";
    public void sendMail(String to, String subject, String randomNum) {
        System.out.println("开始发送邮件:发送给 - >"+ to +",发送的主题 -> "+ subject +",发送的内容 - >"+ content);

        MimeMessage message;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setTo(to);
            helper.setText(content+randomNum, true);
            mailSender.send(message);

            System.out.println("邮件发送成功");
        } catch (MessagingException e) {
            System.out.println("邮件发送失败"+ e);
        }
    }
}
